package com.smalls.newvideotwo.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.media.session.PlaybackStateCompat;
import android.util.Log;

import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.NetSpeed;

public class FloatService extends Service {
    public static String ActionNetSpeedChanged = "actionnetchanged";
    public static String KeyNetSpeedChanged = "keynetchanged";
    public static String mNetSpeed = "0KB/S";
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "FloatService":"FloatService";
    int delaytime = 1000;
    String dev = "eth0";
    private Handler handler = new Handler();
    private BroadcastReceiver mNetReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            FloatService.this.dev = FloatService.getCurrentNetDevice(FloatService.this.getApplicationContext());
            if (FloatService.this.dev == null) {
                FloatService.this.dev = "eth0";
            }
        }
    };
    public String netChange = "android.net.conn.CONNECTIVITY_CHANGE";
    long receiveEnd;
    long receiveStart;
    int state;
    private Runnable task = new Runnable() {
        public void run() {
            FloatService.this.dataRefresh();
            Intent intent1 = new Intent();
            intent1.setAction(FloatService.ActionNetSpeedChanged);
            intent1.putExtra(FloatService.KeyNetSpeedChanged, FloatService.mNetSpeed);
            FloatService.this.sendBroadcast(intent1);
            FloatService.this.handler.postDelayed(this, (long) FloatService.this.delaytime);
        }
    };
    long[] tmp = new long[2];
    long transmitEnd;
    long transmitStart;

    public void dataRefresh() {
        this.receiveStart = this.tmp[0];
        this.transmitStart = this.tmp[1];
        this.tmp = NetSpeed.getSpeed(this.dev);
        if (this.tmp != null && this.tmp.length > 1) {
            this.receiveEnd = this.tmp[0];
            this.transmitEnd = this.tmp[1];
            long speed1 = (this.receiveEnd - this.receiveStart) + (this.transmitEnd - this.transmitStart);
            if (speed1 < 0) {
                mNetSpeed = "0B/S";
            } else if (speed1 > 0 && speed1 < PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID) {
                mNetSpeed = String.valueOf(speed1) + "B/S";
            } else if (speed1 >= 1048576) {
                mNetSpeed = String.valueOf(speed1 / 1048576) + "MB/S";
            } else {
                mNetSpeed = String.valueOf(speed1 / PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID) + "KB/S";
            }
        }
    }

    public void onStart(Intent intent, int startId) {
        Log.d(TAG, "onStart");
        super.onStart(intent, startId);
        IntentFilter mNetFilter = new IntentFilter();
        mNetFilter.addAction(this.netChange);
        mNetFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
        registerReceiver(this.mNetReceiver, mNetFilter);
        this.tmp = NetSpeed.getSpeed(this.dev);
        this.handler.postDelayed(this.task, (long) this.delaytime);
    }

    public void onDestroy() {
        unregisterReceiver(this.mNetReceiver);
        this.handler.removeCallbacks(this.task);
        Log.d(TAG, "onDestroy");
        super.onDestroy();
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    public static String getCurrentNetDevice(Context icontext) {
        ConnectivityManager connectivity = (ConnectivityManager) icontext.getApplicationContext().getSystemService(CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return null;
        }
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info == null) {
            return null;
        }
        int i = 0;
        while (i < info.length) {
            if (!info[i].isConnected()) {
                i++;
            } else if (info[i].getTypeName().equals("WIFI")) {
                return "wlan0";
            } else {
                if (info[i].getTypeName().equals("MOBILE")) {
                    return "ippp0";
                }
                return "eth0";
            }
        }
        return null;
    }
}
